package com.feiyun.common;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice
public class ParamValidatedAdvice {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public String handleBindGetException(MethodArgumentNotValidException exception){

        List<String> errors = exception.getBindingResult()
                .getFieldErrors()
                .stream()
                .map(item -> item.getDefaultMessage())
                .collect(Collectors.toList());
        String result = "参数校验失败:"+ JSONObject.toJSONString(errors);
        return result;
    }
}